<template>
  <div
    :class="className"
    :id="id"
    :style="{height:height,width:width}"
  />
</template>

<script>
// home需要展示的折线图
import echarts from "echarts";
import resize from "./mixins/resize";

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: "chart"
        },
        id: {
            type: String,
            default: "chart"
        },
        width: {
            type: String,
            default: "200px"
        },
        height: {
            type: String,
            default: "200px"
        }
    },
    data() {
        return {
            chart: null
        };
    },
    mounted() {
        this.initChart();
    },
    beforeDestroy() {
        if (!this.chart) {
            return;
        }
        this.chart.dispose();
        this.chart = null;
    },
    methods: {
        initChart() {
            this.chart = echarts.init(document.getElementById(this.id));

            this.chart.setOption({
                backgroundColor: "#FFF",
                // 图表标题
                title: {
                    top: 10,
                    text: "趋势图",
                    textStyle: {
                        fontWeight: "bolder",
                        fontSize: 16,
                        color: "#333"
                    },
                    left: "1.5%"
                },
                tooltip: {
                    trigger: "axis",
                    axisPointer: {
                        lineStyle: {
                            color: "#57617B"
                        }
                    }
                },
                legend: {
                    // 小色块距离顶部距离
                    top: 40,
                    // 小色块形状: 矩形
                    icon: "rect",
                    // 小色块高度
                    itemWidth: 14,
                    // 小色块高度
                    itemHeight: 14,
                    // 小色块间距
                    itemGap: 18,
                    data: [
                        { name: "洗车数量" },
                        { name: "当日营收" },
                        { name: "新增用户" }
                    ],

                    left: "1.5%",
                    textStyle: {
                        fontSize: 12,
                        color: "#333",
                        fontWeight: "bold"
                    }
                },
                grid: {
                    top: 100,
                    left: "2%",
                    right: "2%",
                    bottom: "2%",
                    containLabel: true
                },
                // X轴
                xAxis: [
                    {
                        type: "category",
                        boundaryGap: false,
                        axisLine: {
                            lineStyle: {
                                color: "#FFF"
                            }
                        },
                        axisLabel: {
                            margin: 10,
                            textStyle: {
                                fontSize: 14,
                                color: "#57617B",
                                fontWeight: 700
                            }
                        },
                        data: [
                            "2019-04-28",
                            "2019-04-29",
                            "2019-04-30",
                            "2019-05-01",
                            "2019-05-02",
                            ""
                        ]
                    }
                ],
                // Y轴
                yAxis: [
                    {
                        type: "value",
                        name: "",
                        axisTick: {
                            show: false
                        },
                        // Y轴
                        axisLine: {
                            lineStyle: {
                                color: "#FFF"
                            }
                        },
                        // y轴数字标注
                        axisLabel: {
                            margin: 10,
                            textStyle: {
                                fontSize: 14,
                                color: "#57617B",
                                fontWeight: "bolder"
                            }
                        },
                        // 水平参考线
                        splitLine: {
                            lineStyle: {
                                type: "dashed",
                                color: "#EEE",
                                width: 2
                            }
                        }
                    }
                ],
                series: [
                    {
                        name: "洗车数量",
                        type: "line",
                        smooth: true,
                        symbol: "circle",
                        symbolSize: 5,
                        showSymbol: false,
                        lineStyle: {
                            normal: {
                                width: 2
                            }
                        },
                        // areaStyle: {
                        //     normal: {
                        //         color: new echarts.graphic.LinearGradient(
                        //             0,
                        //             0,
                        //             0,
                        //             1,
                        //             [
                        //                 {
                        //                     offset: 0,
                        //                     color: "rgba(137, 189, 27, 0.3)"
                        //                 },
                        //                 {
                        //                     offset: 0.8,
                        //                     color: "rgba(137, 189, 27, 0)"
                        //                 }
                        //             ],
                        //             false
                        //         ),
                        //         shadowColor: "rgba(0, 0, 0, 0.1)",
                        //         shadowBlur: 10
                        //     }
                        // },
                        itemStyle: {
                            normal: {
                                color: "rgb(137,189,27)",
                                borderColor: "rgba(137,189,2,0.27)",
                                borderWidth: 12
                            }
                        },
                        data: [0, 20, 0, 0, 0]
                    },
                    {
                        name: "当日营收",
                        type: "line",
                        smooth: true,
                        symbol: "circle",
                        symbolSize: 5,
                        showSymbol: false,
                        lineStyle: {
                            normal: {
                                width: 2
                            }
                        },
                        // areaStyle: {
                        //     normal: {
                        //         color: new echarts.graphic.LinearGradient(
                        //             0,
                        //             0,
                        //             0,
                        //             1,
                        //             [
                        //                 {
                        //                     offset: 0,
                        //                     color: "rgba(0, 136, 212, 0.3)"
                        //                 },
                        //                 {
                        //                     offset: 0.8,
                        //                     color: "rgba(0, 136, 212, 0)"
                        //                 }
                        //             ],
                        //             false
                        //         ),
                        //         shadowColor: "rgba(0, 0, 0, 0.1)",
                        //         shadowBlur: 10
                        //     }
                        // },
                        itemStyle: {
                            normal: {
                                color: "rgb(0,136,212)",
                                borderColor: "rgba(0,136,212,0.2)",
                                borderWidth: 12
                            }
                        },
                        data: [0, 20, 0, 0, 20]
                    },
                    {
                        name: "新增用户",
                        type: "line",
                        smooth: true,
                        symbol: "circle",
                        symbolSize: 5,
                        showSymbol: false,
                        lineStyle: {
                            normal: {
                                width: 2
                            }
                        },
                        // areaStyle: {
                        //     normal: {
                        //         color: new echarts.graphic.LinearGradient(
                        //             0,
                        //             0,
                        //             0,
                        //             1,
                        //             [
                        //                 {
                        //                     offset: 0,
                        //                     color: "rgba(219, 50, 51, 0.3)"
                        //                 },
                        //                 {
                        //                     offset: 0.8,
                        //                     color: "rgba(219, 50, 51, 0)"
                        //                 }
                        //             ],
                        //             false
                        //         ),
                        //         shadowColor: "rgba(0, 0, 0, 0.1)",
                        //         shadowBlur: 10
                        //     }
                        // },
                        itemStyle: {
                            normal: {
                                color: "rgb(219,50,51)",
                                borderColor: "rgba(219,50,51,0.2)",
                                borderWidth: 12
                            }
                        },
                        data: [0, 0, 0, 25, 0, 0]
                    }
                ]
            });
        }
    }
};
</script>
